package com.qingluan.darkh.videocontroll;

import android.app.Activity;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.VideoView;

import com.qingluan.darkh.videocontroll.arguments.ARGUMENTS;
import com.qingluan.darkh.videocontroll.service.RecivedBroadcastReceiver;

/**
 * Created by apple on 14/12/3.
 */
public class AtyVideoViewer extends Activity {


    private VideoView vv;
    private RecivedBroadcastReceiver videoControl_broadcaster;



    public void init_videoControl(){
        IntentFilter filter = new IntentFilter();
        videoControl_broadcaster = new RecivedBroadcastReceiver();
        filter.addAction(ARGUMENTS.VIDEO_CONTROL_ACTION);

        videoControl_broadcaster.setReceivedListener(new RecivedBroadcastReceiver.ReceivedListener() {
            @Override
            public void recieved(String info) {
                if(info.equals("pause")){
                    vv.pause();
                }
                else if(info.equals("resume")){
                    vv.resume();
                }
                else if(info.equals("stop")){
                    AtyVideoViewer.this.finish();
                }
            }

        });
        this.registerReceiver(videoControl_broadcaster,filter);

    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        vv = new VideoView(this);
        vv.setMediaController(new MediaController(this));
        vv.setVideoPath(getIntent().getStringExtra(ARGUMENTS.FILE_ROOT_PATH));
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(vv);

        vv.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                unregisterReceiver(videoControl_broadcaster);
                AtyVideoViewer.this.finish();
            }
        });
        vv.start();

    }

}
